/** 带过期时间的缓存 */
export default class DataCache {
  constructor() {
    this.cacheData = Object.create(null)
  }
  setItem(key, value, expires = 0) {
    let _expires = 0
    if (expires > 0) {
      _expires = new Date().getTime() + expires * 1000
    }
    const item = {
      key,
      value,
      expires: _expires
    }
    this.cacheData[key] = item
  }
  getItem(key) {
    if (!(key in this.cacheData)) return false
    const item = this.cacheData[key]
    if (item.expires <= 0 || item.expires > new Date().getTime()) {
      return item.value
    }
    return false
  }
}
